<template>
  <el-container>
    <el-main>
      <div class="default-main ba-table-box">
        <TableHeader
          :buttons="[
            'refresh',
            'add',
            'edit',
            'remove',
            'comSearch',
            'columnDisplay',
          ]"
          :quick-search-placeholder="placeholder"
          @action="baTable.onTableHeaderAction"
        />
        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" @action="baTable.onTableAction" />
        <!-- 表单 -->
        <PopupForm ref="formRef" v-if="baTable.form.operate" />
      </div>
    </el-main>
  </el-container>
</template>
<script lang="ts" setup>
import { ref, provide, onMounted, markRaw } from "vue";
import baTableClass from "@/utils/baTable";
import { baTableApi, operatorShowOff, DictOperation } from "@/utils/common"; // 导入表格api方法生成器
import Table from "@/components/table/index.vue";
import TableHeader from "@/components/table/header/index.vue";
import useCurrentInstance from "@/utils/useCurrentInstance";
import type { TableRow, TableColumn } from "@/utils/baTable"; //表格列行类型
import { WarningNoticeConfig } from "@/api/backend/index";
import PopupForm from "./popupForm.vue";
import { useRoute, useRouter } from "vue-router";
import { useNavTabs } from "@/store/navTabs";
import { useWarningNoticeConfig } from "@/store/backend/Warning/noticeConfig";
import { useDictionary } from "@/store/backend/tool/dictionary";
import { useAssetSite } from "@/store/backend/Assets/site";
import TagSearch from "@/components/tagSearch/index.vue";
import Tag from "./components/tag.vue";
import { useUser } from "@/store/backend/SysM/user";

const placeholder = ref<string>("模糊搜索");
const tableRef = ref();
const route = useRoute();
const router = useRouter();
const navTabs = useNavTabs();
const noticeConfig = useWarningNoticeConfig();
const dictionary = useDictionary();
const site = useAssetSite();
const user = useUser();
const formRef = ref();

//字段显隐记忆功能
const filedShowOff = (filter: any) => {
  if ((navTabs as any).columnDisplayStoreMap) {
    return (navTabs as any).columnDisplayStoreMap.get(
      route.path + "/" + filter
    );
  }
  return true;
};
// 每页个数记忆功能
const pageSizeKeep = () => {
  if ((navTabs as any).pageSizeMap) {
    return (navTabs as any).pageSizeMap.get(route.path + WarningNoticeConfig)
      ? (navTabs as any).pageSizeMap.get(route.path + WarningNoticeConfig)
      : 10;
  }
  return 10;
};

//初始化字典处理类
const dictOperation = new DictOperation();

//全局事件总线
const { proxy } = useCurrentInstance();
const baTable = new baTableClass(
  new baTableApi(WarningNoticeConfig), //this.api = api菜单管理相关
  {
    pk: "noticeConfigId",
    column: [
      { type: "selection", align: "center", operator: false },
      {
        show: filedShowOff("noticeConfigName"),
        label: "配置名称",
        prop: "noticeConfigName",
        align: "center",
        operatorPlaceholder: "模糊搜索",
      },
      {
        show: filedShowOff("noticeCycle"),
        label: "通知周期",
        prop: "noticeCycle",
        align: "center",
        operator: false,
      },
      {
        show: filedShowOff("noticeStartTime"),
        label: "通知时段起始时间",
        prop: "noticeStartTime",
        align: "center",
        operator: false,
      },
      {
        show: filedShowOff("noticeEndTime"),
        label: "通知时段结束时间",
        prop: "noticeEndTime",
        align: "center",
        operator: false,
      },
      {
        show: filedShowOff("userName"),
        label: "接收用户",
        prop: "userName",
        align: "center",
        operator: false,
      },
      {
        show: filedShowOff("nickName"),
        label: "接收用户别名",
        prop: "nickName",
        align: "center",
        operator: false,
      },
      {
        show: filedShowOff("noticeWay"),
        label: "通知方式",
        prop: "noticeWay",
        align: "center",
        render: "customRender",
        customRender: markRaw(Tag),
        // comSearchRender: "customRender",
        // comSearchCustomRender: markRaw(TagSearch),
        operator: false,
      },
      {
        show: filedShowOff("noticeType"),
        label: "通知类型",
        prop: "noticeType",
        align: "center",
        render: "customRender",
        customRender: markRaw(Tag),
        comSearchRender: "customRender",
        comSearchCustomRender: markRaw(TagSearch),
      },
      {
        show: operatorShowOff([route.path + "/edit", route.path + "/remove"]),
        label: "操作",
        align: "center",
        width: "130",
        render: "buttons",
        fixed: "right",
        operator: false,
        buttons: [
          {
            render: "tipButton",
            name: "edit",
            title: "编辑",
            text: "",
            type: "primary",
            icon: "iconfont icon-pencil",
            class: "table-row-edit",
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
          {
            render: "confirmButton",
            name: "remove",
            title: "删除",
            text: "",
            type: "danger",
            icon: "el-icon-DeleteFilled",
            class: "table-row-delete",
            popconfirm: {
              confirmButtonText: "删除",
              cancelButtonText: "取消",
              confirmButtonType: "danger",
              title: "确定删除所选项吗",
            },
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
        ],
      },
    ],
    filter: {
      pageSize: pageSizeKeep(),
    },
  },
  {
    // 默认表单数据(添加)
    defaultItems: {
      cabinetType: 0, //立式
    },
  }
);

//跳转机柜详情页 历史记录数据
// const infoButtonClick = (name: string, row: any) => {
//   console.log("跳转机柜详情页", row);
//   category.$patch((state: any) => {
//     state.single = row;
//   });
//   router.push({
//     path: "/admin/WarningNoticeConfig/detail",
//     query: { cabinetId: row.cabinetId },
//   });
// };

onMounted(() => {
  baTable.table.ref = tableRef.value;
  baTable.mount();
  baTable.getList(WarningNoticeConfig);

  /**
   * 表格内的按钮响应
   * @param name 按钮name
   * @param row 被操作行数据
   */
  proxy.eventBus.on(
    "onTableButtonClick",
    (data: { name: string; row: TableRow }) => {
      if (!baTable.activate) return;
      //回滚功能暂无
      if (data.name == "rollback") {
        // onRollback([data.row[baTable.table.pk!]])
      } else if (data.name == "info") {
        //携带主键字段pk
        // infoButtonClick(
        //   (data.row as TableRow)[baTable.table.pk!],
        //   data.row as any
        // );
      }
    }
  );
});

//获取字典
const getDictionary = () => {
  //获取告警通知类型的字典
  dictOperation.getDictionary(
    noticeConfig,
    "sys_warningnotice_type",
    "noticeType",
    "type"
  );
  //获取告警通知方式的字典
  dictOperation.getDictionary(
    noticeConfig,
    "sys_warningnotice_way",
    "noticeWay",
    "typeWay"
  );
};
getDictionary();

// 实例化表格后，将 baTable 的实例提供给上下文
provide("baTable", baTable);
provide("site", site);
provide("dictionary", dictionary);
provide("noticeConfig", noticeConfig);
provide("user", user);
</script>
<style lang="scss" scoped>
.el-main {
  padding: 0;
}
.default-main {
  margin: var(--ba-main-space) var(--ba-main-space) 60px var(--ba-main-space);
}
</style>
